﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Forum.BLL.Member;
using Forum.Entity.Member;
using Forum.BLL.Group;
using Forum.Entity.Group;
using Frame.Lib.Common;
using Frame.Lib.Web;
using Frame.Lib.Enums;
using Frame.Lib.Utilities;
using Forum.Common;


namespace Forum.Web.Admin.UForum.Member
{
    public partial class Edit : PageEdit
    {
        private MemberExBO ForumMember
        {
            get
            {
                return MemberExBO.Instance<MemberExBO>();
            }
        }
        private MemberBaseBO MemberBase
        {
            get
            {
                return MemberBaseBO.Instance<MemberBaseBO>();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            CssFileManager.AddCssFile(CssFile.tab2);
            if (!IsPostBack)
            {
                WebUtil.BindListControlWithEnum(this.State, typeof(MemberState));
                WebUtil.BindListControlWithEnum(this.Gender, typeof(Gender));
                GroupBO group = new GroupBO();
                IEnumerable<GroupInfo> list = group.GetList().OrderBy(T => T.TypeID);
                GroupID.DataValueField = "ID";
                GroupID.DataTextField = "Name";
                GroupID.DataSource = list;
                GroupID.DataBind();

                BindList();

                if (!string.IsNullOrEmpty(PKID))
                {
                    DispInfo();
                }

            }
            AddAttr();
        }

        private void AddAttr()
        {
            WebUtil.SetTimeBox(Birthday, false);
        }

        private void DispInfo()
        {
            MemberExInfo info = this.ForumMember.GetInfo(this.PKID);

            EntityUtil.SetControl(pnlMain, info);
            if (info.Birthday != null)
            {
                Birthday.Text = info.Birthday.Value.ToString("yyyy-MM-dd");
            }

            txtPassword.Attributes.Add("value", info.MemberBaseInfo.Password);
            hidPassword.Value = info.MemberBaseInfo.Password;

            this.LoginName.Text = info.MemberBaseInfo.LoginName;
            this.Name.Text = info.MemberBaseInfo.Name;
            this.Email.Text = info.MemberBaseInfo.Email;
            this.Mobile.Text = info.MemberBaseInfo.Mobile;

        }

        private void BindList()
        {
            List<EnumInfo> list = ConvertUtil.ConvertEnum(typeof(MemberTab));
            rpt.DataSource = list;
            rpt.DataBind();

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string loginName = LoginName.Text.Trim();
            string mobile = this.Mobile.Text.Trim();
            string email = this.Email.Text.Trim();
            bool flag = this.MemberBase.ExistField(TableField.LoginName, loginName, PKID);
            if (flag)
            {
                JSUtil.Alert("用户名已存在");
                return;
            }

            flag = this.MemberBase.ExistField("Mobile", mobile, PKID);
            if (flag)
            {
                JSUtil.Alert("该手机已被使用");
                return;
            }

            flag = this.MemberBase.ExistField("Email", email, PKID);
            if (flag)
            {
                JSUtil.Alert("该电子邮件已被使用");
            }

            this.MemberBase.dbOper = this.ForumMember.dbOper;
            this.MemberBase.dbOper.BeginTrans();

            try
            {
                //保存主表
                if (this.Name.Text.Trim() == "")
                {
                    this.Name.Text = this.LoginName.Text;
                }
                MemberBaseInfo baseInfo = this.MemberBase.GetInfo(this.PKID);
                EntityUtil.SetEntity(pnlMain, baseInfo);
               

                if (txtPassword.Text.Trim() != hidPassword.Value)
                {
                    baseInfo.Password = StrUtil.EncryptDES(txtPassword.Text.Trim());
                }
                this.MemberBase.Save(baseInfo);


                //保存从表
                MemberExInfo forumInfo = this.ForumMember.GetInfo(this.PKID);
                EntityUtil.SetEntity(pnlMain, forumInfo);

                forumInfo.GroupName = GroupID.SelectedItem.Text;

                if (string.IsNullOrEmpty(PKID))
                {
                    forumInfo.ID = baseInfo.ID;
                    forumInfo.RegisterTime = forumInfo.LastPostTime = forumInfo.LastVisitTime = DateTime.Now;
                    this.ForumMember.Save(forumInfo, true);
                }
                else
                {
                    this.ForumMember.Save(forumInfo, false);
                }

                this.MemberBase.dbOper.CommitTrans();
                JSUtil.RefreshView();
            }
            catch (Exception ex)
            {
                this.MemberBase.dbOper.RollbackTrans();
                throw ex;
            }
        }
    }
}